package com.icss.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.icss.model.User;
import com.icss.service.UserService;
import com.icss.service.impl.UserServiceImpl;
import com.icss.util.ValidateCode;


@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	UserService us=new UserServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String flag=request.getParameter("flag");
		if("loginOut".equals(flag)) {
			loginOut(request,response);
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String flag=request.getParameter("flag");
		if("findByName".equals(flag)) {
			findByName(request,response);
		}
		if("findByTel".equals(flag)) {
			findByTel(request,response);
		}
		if("findByPass".equals(flag)) {
			findByPass(request,response);
		}
		if("reg".equals(flag)) {
			reg(request,response);
		}
		if("login".equals(flag)) {
			login(request,response);
		}
		
	}
	private void loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Cookie[] c = request.getCookies();
		if(c != null && c.length > 0) {
			for(int i = 0;i < c.length;i++) {
				if("user".equals(c[i].getName())) {
					c[i].setMaxAge(0);
					c[i].setPath("/");
					response.addCookie(c[i]);
				}
				if("pass".equals(c[i].getName())) {
					c[i].setMaxAge(0);
					c[i].setPath("/");
					response.addCookie(c[i]);
				}
			}
		}
		response.sendRedirect("login.jsp");
	}
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("user");
		String pass = request.getParameter("pass");
		String code = request.getParameter("code");
		String rm = request.getParameter("rm");
		if(name != null && !"".equals(name) && pass != null && !"".equals(pass)) {
			User u = us.login(name, pass);
			if(u == null) {
				request.setAttribute("msg", "用户名和密码不正确");
				request.getRequestDispatcher("login.jsp").forward(request, response);
				return;
			}else {
				//登录正确
				HttpSession session = request.getSession();
				String randomCode = (String) session.getAttribute(ValidateCode.RANDOMCODEKEY);
				if(!code.equalsIgnoreCase(randomCode)) {
					request.setAttribute("msg", "验证码有误！");
					request.getRequestDispatcher("login.jsp").forward(request, response);
					return;
				}else {
					//用户名密码正确，验证码也是正确的
					Cookie namec = new Cookie("user", name);
					Cookie passc = new Cookie("pass", pass);
					if(rm != null) {
						namec.setMaxAge(7*24*60*60);
						passc.setMaxAge(7*24*60*60);
						namec.setPath("/");
						passc.setPath("/");
					}else {
						namec.setMaxAge(0);
						passc.setMaxAge(0);
						namec.setPath("/");
						passc.setPath("/");
					}
					response.addCookie(namec);
					response.addCookie(passc);
					ServletContext sc=this.getServletContext();
					session.setAttribute("user", u);
					request.getRequestDispatcher("index.jsp").forward(request, response);
				}
			}
		}else {
			request.setAttribute("msg", "用户名和密码不能为空！");
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return;
		}
		
	}
	private void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pass=request.getParameter("pass");
		String user=request.getParameter("user");
		String tel=request.getParameter("tel");
		String type = request.getParameter("type");
		int t = 1;
		if(type != null && !"".equals(type)) {
			t = Integer.parseInt(type);
		}
		boolean re=us.reg(new User(0,user,pass,tel,t,0));
		if(re) {
			request.setAttribute("msg", "注册成功，请登录！！");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}else {
			request.setAttribute("msg", "注册失败，请重新注册！");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		
	}
	private void findByPass(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String passName=request.getParameter("passName");
		response.setContentType("text/html; charset=UTF-8");
		if(passName!=null&&!"".equals(passName)) {
			response.getWriter().print("可用");
		}else {
			response.getWriter().print("不可用");
		}
	}
	private void findByTel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String telName=request.getParameter("telName");
		//判断手机号长度为11并且为纯数字
		String reg = "1[0-9]{10}";
		User u=us.findByUserTel(telName);
		response.setContentType("text/html; charset=UTF-8");
		if(u == null && telName.matches(reg)) {
			response.getWriter().print("可用");
		}else {
			response.getWriter().print("不可用");
		}
		
	}
	private void findByName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userName=request.getParameter("userName");
		User u=us.findByUserName(userName);
		response.setContentType("text/html; charset=UTF-8");
		if(u == null &&userName!=null&&!"".equals(userName)) {
			response.getWriter().print("可用");
		}else {
			response.getWriter().print("不可用");
		}
	}

}
